//
//  LHStatusRetweetView.swift
//  sina_weibo
//
//  Created by 刘刘海强 on 16/5/13.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit
import SnapKit

class LHStatusRetweetView: UIView {
    
    var retweetViewBotton: Constraint?
    
    var statusViewModel: LHStatusViewModel? {
        didSet {
           messageLabel.text = statusViewModel?.retweetContent
            
            //卸载约束
            retweetViewBotton?.uninstall()
            //判断配图存在
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls where picUrls.count > 0 {
                //显示view，更新约束
                statusPictureView.picUrl = picUrls
                statusPictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBotton = make.bottom.equalTo(statusPictureView.snp_bottom).offset(StatusTableViewCellMargin).constraint
                })
            } else {
                //隐藏view，更新约束
                statusPictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBotton = make.bottom.equalTo(messageLabel.snp_bottom).offset(StatusTableViewCellMargin).constraint
                })
            }
        }
    }

    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.grayColor(), fontSize: 14)
        label.numberOfLines = 0
        return label
    }()
    
    //微博配图
    private lazy var statusPictureView: LHStatusPictureView = {
        let pictureView = LHStatusPictureView()
        return pictureView
    }()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        addSubview(messageLabel)
        addSubview(statusPictureView)
        
        //设置约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        statusPictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(messageLabel)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBotton = make.bottom.equalTo(statusPictureView.snp_bottom).offset(StatusTableViewCellMargin).constraint
        }
    }
}
